package com.unionftech.common.webview.view

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import com.unionftech.common.webview.impl.IndicatorHandler

class ProgressWebView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : BaseWebView(context, attrs, defStyleAttr) {
    private var indicatorHandler: IndicatorHandler? = null
    private var progressBar: WebProgressBar = WebProgressBar(context)
    private val mHandler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            val progress = msg.obj as Int
            indicatorHandler?.progress(progress)
        }
    }

    init {
        initView()
    }

    private fun initView() {
        progressBar.layoutParams =
            LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        progressBar.visibility = GONE
        addView(progressBar)
        indicatorHandler = IndicatorHandler.getInstance().inJectProgressView(progressBar)
        webChromeClient = BaseWebChromeClient(mHandler)
    }
}